using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The JobDesignationCodeTableDB class is responsible for interacting with the database to retrieve and store information
/// about JobDesignationCodeTable objects.
/// </summary>
public class JobDesignationCodeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of JobDesignationCodeTable from the underlying datasource.
/// </summary>
/// <param name="jobDesignationCode">The unique JobDesignationCode of the JobDesignationCodeTable in the database.</param>
/// <returns>An JobDesignationCodeTable when the JobDesignationCode was found in the database, or null otherwise.</returns>
public static JobDesignationCodeTable GetItem(Int16 jobDesignationCode)
{
JobDesignationCodeTable myJobDesignationCodeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocJobDesignationCodeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@jobDesignationCode", jobDesignationCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myJobDesignationCodeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myJobDesignationCodeTable;
}
}

/// <summary>
/// Returns a list with JobDesignationCodeTable objects.
/// </summary>
/// <returns>A generics List with the JobDesignationCodeTable objects.</returns>
public static JobDesignationCodeTableList GetList()
{
JobDesignationCodeTableList tempList = new JobDesignationCodeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocJobDesignationCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a JobDesignationCodeTable in the database.
/// </summary>
/// <param name="myJobDesignationCodeTable">The JobDesignationCodeTable instance to save.</param>
/// <returns>The new JobDesignationCode if the JobDesignationCodeTable is new in the database or the existing JobDesignationCode when an item was updated.</returns>
public static Int16 Save(JobDesignationCodeTable myJobDesignationCodeTable)
{
    Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocJobDesignationCodeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myJobDesignationCodeTable.JobDesignationCode == -1)
{myCommand.Parameters.AddWithValue("@jobDesignationCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@jobDesignationCode", myJobDesignationCodeTable.JobDesignationCode);
}
if (myJobDesignationCodeTable.CategoryTypeCode == null){
myCommand.Parameters.AddWithValue("@categoryTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@categoryTypeCode", myJobDesignationCodeTable.CategoryTypeCode);
}
if (myJobDesignationCodeTable.Description == null){
myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@description", myJobDesignationCodeTable.Description);
}
if (myJobDesignationCodeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myJobDesignationCodeTable.Status);
}
if (myJobDesignationCodeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myJobDesignationCodeTable.CreatedDateTime);
}
if (myJobDesignationCodeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myJobDesignationCodeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a JobDesignationCodeTable from the database.
/// </summary>
/// <param name="jobDesignationCode">The JobDesignationCode of the JobDesignationCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(Int16 jobDesignationCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocJobDesignationCodeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@jobDesignationCode", jobDesignationCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the JobDesignationCodeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static JobDesignationCodeTable FillDataRecord(IDataRecord myDataRecord )
{
JobDesignationCodeTable myJobDesignationCodeTable = new JobDesignationCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("JobDesignationCode")))
{
myJobDesignationCodeTable.JobDesignationCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("JobDesignationCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CategoryTypeCode")))
{
myJobDesignationCodeTable.CategoryTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CategoryTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myJobDesignationCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myJobDesignationCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myJobDesignationCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myJobDesignationCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myJobDesignationCodeTable;
}
}
